﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using Golos.Helper;
using Golos.Http;
using Golos.Polls.PollCache;

namespace Golos.Polls.Http
{
    public partial class PollCacheServer : HttpServer
    {
        public PollCacheServer(int port): base(port)
        {
            pollCache = new Cache();
            LogClass.WriteInfoEntry(string.Format("Listening on http://*:{0}/", port));
        }

        protected override void IncomingRequest(HttpListenerContext context, ref int StatusCode)
        {
            string method = context.Request.HttpMethod;
            switch (method.ToUpperInvariant())
            {
                case "GET":
                    Get(context, ref StatusCode);
                    break;
                case "POST":
                    Post(context, ref StatusCode);
                    break;
                case "DELETE":
                    Delete(context, ref StatusCode);
                    break;
                default:
                    break;
            }
        }

        private void Delete(HttpListenerContext context, ref int statusCode)
        {
            string operation = context.Request.QueryString["operation"];
            switch (operation)
            {
                
                case "deletequestion":
                    DeleteQuestion(context, ref statusCode);
                    break;
                default:
                    break;
            }
        }

        private void Post(HttpListenerContext context, ref int statusCode)
        {
            string operation = context.Request.QueryString["operation"];
            switch (operation)
            {
                case "createpoll":
                    CreateNewPoll(context, ref statusCode);
                    break;
                case "vote":
                    Vote(context, ref statusCode);
                    break;
                case "mergeanswers":
                    MergeAnswers(context, ref statusCode);
                    break;
                default:
                    break;
            }
        }

        private void Get(HttpListenerContext context, ref int statusCode)
        {
            string operation = context.Request.QueryString["operation"];
            switch (operation)
            {
                case "top10question":
                    GetTop10Question(context, ref statusCode);
                    break;
                case "poll":
                    GetPoll(context, ref statusCode);
                    break;
                case "checkuseranswer":
                    CheckUserAnswer(context, ref statusCode);
                    break;
                case "voicescount":
                    GetVoicesCount(context, ref statusCode);
                    break;
                case "distribution":
                    GetDistribution(context, ref statusCode);
                    break;
                case "myquestion":
                    GetMyQuestion(context, ref statusCode);
                    break;
                case "searchpoll":
                    SearchPoll(context, ref statusCode);
                    break;
                default:
                    break;
            }
        }

        private Cache pollCache;
    }
}
